package com.hxht.cmp.controller;

import com.hxht.cmp.common.Constants;
import com.hxht.cmp.common.enums.ResultCode;
import com.hxht.cmp.common.result.ResultResponse;
import com.hxht.cmp.entity.CaseType;
import com.hxht.cmp.entity.Court;
import com.hxht.cmp.entity.CourtRoom;
import com.hxht.cmp.entity.User;
import com.hxht.cmp.pojo.BasisCourt;
import com.hxht.cmp.service.BasisService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author alice on 2017/6/29 0029.
 * @version 1.0
 * @since 1.0
 */
@Controller
@RequestMapping("/basis")
public class BasisController {
    private final Logger log = LoggerFactory.getLogger(BasisController.class);
    @Resource
    private BasisService basisService;

    /**
     * 刷新基础配置.
     * 刷新内容为config
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushDict", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse flushDict() {
        return basisService.flushDict() ? ResultResponse.success() : ResultResponse.failed();
    }

    /**
     * 刷新全部基础配置.
     * 刷新内容包括config,法院列表,法庭列表,案件类型,角色列表
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushAll", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse flushAll() {
        try {
            Boolean success = basisService.flushDict();
            ResultResponse resultResponse1 = flushCourt();
            Boolean success1 = basisService.flushCourtConfig();
            ResultResponse resultResponse2 = flushCourtroom();
            ResultResponse resultResponse3 = flushCaseType();
            ResultResponse resultResponse4 = flushRole();
            Map<String, Integer> result = new LinkedHashMap<>();
            result.put("flushDict", success ? 0 : 1);
            result.put("flushCourtConfig", success1 ? 0 : 1);
            result.put("flushCourt", resultResponse1.getCode());
            result.put("flushCourtroom", resultResponse2.getCode());
            result.put("flushCaseType", resultResponse3.getCode());
            result.put("flushRole", resultResponse4.getCode());
            return ResultResponse.success(result);
        }catch (Exception e){
            Constants.SYSTEM_LOCKED = 1;
            return ResultResponse.failed(ResultCode.SERVER_ERROR);
        }

    }

    /**
     * 刷新指定基础配置.
     * 刷新内容包括,法院列表
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushCourt", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse flushCourt() {
        synchronized (this) {
            try {
                Constants.COURT_LIST = basisService.getCourt();
                Constants.COURT_MAP.clear();
                for (Court court : Constants.COURT_LIST) {
                    Constants.COURT_MAP.put(court.getUid(), court);
                }
                return ResultResponse.success();
            } catch (Exception e) {
                log.error("刷新基础配置_法院列表时发生严重错误,请检查!");
                return ResultResponse.init(1, "刷新法院列表错误");
            }
        }
    }

    /**
     * 刷新指定基础配置.
     * 刷新内容包括,法庭列表
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushCourtroom", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse flushCourtroom() {
        synchronized (this) {
            try {
                Constants.COURTROOM_LIST = basisService.getCourtRoom(null);
                for (CourtRoom courtRoom : Constants.COURTROOM_LIST) {
                    Constants.COURTROOM_MAP.put(courtRoom.getUid(), courtRoom);
                }
                return ResultResponse.success();
            } catch (Exception e) {
                log.error("刷新基础配置_法院法庭列表时发生严重错误,请检查!");
                return ResultResponse.init(1, "刷新法院法庭列表错误");
            }
        }
    }

    /**
     * 刷新指定基础配置.
     * 刷新内容包括,案件类型
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushCaseType", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse flushCaseType() {
        synchronized (this) {
            try {
                Constants.CASE_TYPE_LIST = basisService.getCaseType(null);
                for (CaseType caseType : Constants.CASE_TYPE_LIST) {
                    Constants.CASE_TYPE_MAP.put(caseType.getUid(), caseType);
                }
                return ResultResponse.success();
            } catch (Exception e) {
                log.error("刷新基础配置_案件类型列表时发生严重错误,请检查!");
                return ResultResponse.init(1, "刷新案件类型列表错误");
            }
        }
    }

    /**
     * 刷新指定基础配置.
     * 刷新内容包括,角色列表
     *
     * @return ResultResponse
     */
    @RequestMapping(value = "/flushRole", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse flushRole() {
        synchronized (this) {
            try {
                Constants.ROLE_LIST = basisService.getRole(null);
                return ResultResponse.success();
            } catch (Exception e) {
                log.error("刷新基础配置_角色列表时发生严重错误,请检查!");
                return ResultResponse.init(1, "刷新角色列表错误");
            }
        }
    }

    @ModelAttribute("courts")
    public List<Court> courts() {
        Subject currentUser = SecurityUtils.getSubject();
        if (currentUser.isAuthenticated()) {
            List<Court> result = new ArrayList<>();
            Session session = currentUser.getSession();
            User user = (User) session.getAttribute("user");
            Court court = Constants.COURT_MAP.get(user.getCourtUid());
            String courtUid = court.getUid();
            if (!Objects.equals(courtUid, Constants.CENTRAL_COURT.getUid())) {
                for (Court tempCourt : Constants.COURT_LIST) {
                    if (Objects.equals(courtUid, tempCourt.getUid()) || Objects.equals(court.getUid(), tempCourt.getParentUid())) {
                        result.add(tempCourt);
                    }
                }
                return result;
            }else {
              return Constants.COURT_LIST;
            }
        }
        return null;
    }

    @RequestMapping(value = "/getCourtList", method = RequestMethod.GET)
    public @ResponseBody
    ResultResponse getCourtList(String courtUid) {
        if (ObjectUtils.isEmpty(courtUid)) {
            return ResultResponse.failed(ResultCode.PARAMETER_NULL);
        }
        List<BasisCourt> result = new ArrayList<>();
        Court court = Constants.COURT_MAP.get(courtUid);
        if (!Objects.equals(courtUid, Constants.CENTRAL_COURT.getUid())) {
            for (BasisCourt tempCourt : Constants.COURT_CONFIG_LIST) {
                if (Objects.equals(courtUid, tempCourt.getUid()) || Objects.equals(court.getUid(), tempCourt.getParentUid())) {
                    result.add(tempCourt);
                }
            }
        }else {
            return ResultResponse.success(Constants.COURT_CONFIG_LIST);
        }
        return ResultResponse.success(result);
    }
}
